//
//  BusinessDetailsTableViewController.swift
//  ZNHXManagement
//
//  Created by 四川中农 on 2018/9/29.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import HandyJSON
import DZNEmptyDataSet

class BusinessDetailsTableViewController: BaseTableViewController {
    
    var type = 0
    var dataArray = [OrdersModel]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "业务详情"
        self.tableView.rowHeight = 60
        self.getOrder()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OrderDetailStateTableViewCell", for: indexPath) as! OrderDetailStateTableViewCell
        cell.steteLabel.isHidden = true
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "OrderDetailViewController") as! OrderDetailViewController
        self.navigationController?.pushViewController(ctrl, animated: true)
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if self.dataArray.count != 0 {
            var header:BusinessDetailheader?
            header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "BusinessDetailheader") as? BusinessDetailheader
            if header == nil {
                header = BusinessDetailheader.init(reuseIdentifier: "BusinessDetailheader")
            }
            if self.type == 0 {
                header?.titleLabel.text = "\(header?.getMonth(date: Date().passDate()) ?? 0)" + "月累计：" + "\(self.dataArray.count)单"
            }else{
                header?.titleLabel.text = "\(header?.getMonth(date: Date().passDate().getExpectTimestamp(year: nil, month: -1, day: nil)) ?? 0)" + "月累计：" + "\(self.dataArray.count)单"
            }
            return header
        }else{
            return UIView()
        }
    }
}

extension BusinessDetailsTableViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate {
    func getOrder(){
        PKhud.show()
        HttpRequest.requestData(.post, URLString: Api.userOrder.rawValue, parameters: ["type":self.type]) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                if let array = [OrdersModel].deserialize(from: result["data"] as? [Any]) as? [OrdersModel] {
                    self.dataArray = array
                    self.tableView.reloadData()
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let str = "空空如也~"
        let attribute = NSMutableAttributedString.init(string: str)
        attribute.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14),
                                 NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "666666")], range: NSRange.init(location: 0, length: str.count))
        
        return attribute
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return UIImage.init(named: "img_Defaultheadimage")
    }
}












